package it.lovaex.scb.Cart;

import it.lovaex.scb.factory.IDiscountRateFactory;
import it.lovaex.scb.model.IItem;

import java.math.BigDecimal;
import java.util.ArrayList;

public class Cart {

	private ArrayList<CartItem> itemsList;
	private IDiscountRateFactory discountFactory;
	
	public Cart(IDiscountRateFactory discountFactory){
		this.discountFactory = discountFactory;
		itemsList = new ArrayList<CartItem>();
	}
	
	public ArrayList<CartItem> getItemsOnCart(){
		return itemsList;
	}
	
	public void empty(){
		itemsList.clear();
	}
	
	public boolean isEmpty(){
		return itemsList.isEmpty();
	}
	
	public void add(int quantity ,IItem item){
		itemsList.add(new CartItem(quantity, item, discountFactory.getByItemCode(item.getItemCode())));
	}
	
	public BigDecimal getFinalTotal(){
		BigDecimal rawTotal = getRawTotal();
		return Utils.CalculateDiscountedPrice(rawTotal, discountFactory.getByTotal(rawTotal));
	}
	
	public BigDecimal getRawTotal(){
		BigDecimal rawTotal = BigDecimal.valueOf(0);
		for(CartItem cartItem : itemsList){
			rawTotal = rawTotal.add(cartItem.getFinalPrice());
		}
		return rawTotal;
	}
	
	public Receipt getReceipt(){
		BigDecimal rawTotal = getRawTotal();
		return new Receipt(itemsList, rawTotal, getFinalTotal(), discountFactory.getByTotal(rawTotal));
	}
}
